//
//  DayCalendarViewController.m
//  商城
//
//  Created by runone02 on 2018/11/7.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "DayCalendarViewController.h"

#import "CalendarHomeViewController.h"

@interface DayCalendarViewController (){
    
    
}
/** */
@property (nonatomic,strong) CalendarHomeViewController *chvc;
@property (nonatomic,strong) NSMutableArray * dataArray;
@property (nonatomic,strong) UIView * mainView;
@end

@implementation DayCalendarViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    __weak __typeof(self)weakSelf = self;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"入住/离店日期";
    
    self.dataArray = [[NSMutableArray alloc] init];
    
    self.chvc = [[CalendarHomeViewController alloc]init];
    self.chvc.view.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height);
    
    [self.view addSubview:self.chvc.view];
    
//    [self.chvc setAirPlaneToDay:900 ToDateforString:nil];//飞机初始化方法
    [self.chvc setHotelToDay:900 ToDateforString:nil ];  //酒店
//    [self.chvc setTrainToDay:900 ToDateforString:nil ];  //火车
    
    [self mainViewClass:0];
    
    self.chvc.calendarblock = ^(CalendarDayModel *model){
        
        if(model.style == CellDayTypeClick){
            [weakSelf.dataArray addObject:model.toString];
            
            NSSet *set = [NSSet setWithArray:weakSelf.dataArray];
            weakSelf.dataArray = [[set allObjects] mutableCopy];
            
            [weakSelf.dataArray sortUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
                return [obj1 compare:obj2];
            }];
        }
        else{
            [weakSelf.dataArray removeObject:model.toString];
            
        }        
        [weakSelf mainViewClass:weakSelf.dataArray.count];
    };
}

- (void)mainViewClass:(NSInteger)num{
    
    [self.mainView removeFromSuperview];
    
    self.mainView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-50,self.view.frame.size.width,50)];
    self.mainView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.mainView];
    
    UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    lable.font = [UIFont systemFontOfSize:14.0f];
    lable.textColor = [UIColor whiteColor];
    lable.textAlignment = NSTextAlignmentCenter;
    [self.mainView addSubview:lable];
    
    if(num == 0){
        lable.text = @"请选择入住时间";
    }
    if(num == 1){
        lable.text = @"请选择离店时间";        
    }
    if(num == 2){
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"YYYY-MM-dd"];
        NSDate* date1 = [formatter dateFromString:[self.dataArray objectAtIndex:0]];
        NSDate* date2 = [formatter dateFromString:[self.dataArray objectAtIndex:1]];
        
        
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *comps = [gregorian components:NSCalendarUnitDay fromDate:date1 toDate:date2  options:0];
        
        NSInteger days = [comps day];
        
        lable.text = [NSString stringWithFormat:@"%@入住---%@离店 共%ld晚",[self.dataArray objectAtIndex:0],[self.dataArray objectAtIndex:1],days];
    }
}

@end
